package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IPrdFreeFlowService;
import com.wondertek.poms.dao.po.PrdFreeFlow;
import com.wondertek.poms.service.util.BeanPropertiesUtil;
import com.wondertek.poms.service.vo.PrdFreeFlowVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author evan0213
 * @description
 */
@RestController
@RequestMapping("prd-info-free-flow")
public class PrdInfoFreeFlowController {

	@Autowired
	private IPrdFreeFlowService prdFreeFlowService;
	@Autowired
	private BeanPropertiesUtil beanPropertiesUtil;

	@ApiOperation(value = "商品包免流量 列表", notes = "商品包免流量 列表")
	@GetMapping("/list")
	@MethodLog(module = "商品包免流量列表")
	public ResultBean list(Long prdInfoId) {
		if (prdInfoId == null) {
			return ResultBean.error("商品包id不能为空");
		}
		List<PrdFreeFlow> list = prdFreeFlowService.findAllByPomsIdAndPomsType(prdInfoId, 1);
		return ResultBean.ok(list);
	}

	@ApiOperation(value = "商品包免流量 新增", notes = "商品包免流量 新增")
	@PostMapping("/add")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pomsId", required = true, value = "商品包id"),
			@ApiImplicitParam(name = "yearFrom", required = false, value = "从何年开始"),
			@ApiImplicitParam(name = "yearTo", required = false, value = "到何年结束"),
			@ApiImplicitParam(name = "monthFrom", required = false, value = "从几月开始"),
			@ApiImplicitParam(name = "monthTo", required = false, value = "到几月结束(包含)"),
			@ApiImplicitParam(name = "weekFrom", required = false, value = "从周几开始"),
			@ApiImplicitParam(name = "weekTo", required = false, value = "到周几结束(包含)"),
			@ApiImplicitParam(name = "dayFrom", required = false, value = "从几日开始"),
			@ApiImplicitParam(name = "dayTo", required = false, value = "到几日结束(包含)"),
			@ApiImplicitParam(name = "timeFrom", required = false, value = "从几点开始"),
			@ApiImplicitParam(name = "timeTo", required = false, value = "到几点结束(包含)")
	})
	@MethodLog(module = "商品包免流量新增", insertDB = true, opterate = 1)
	public ResultBean add(@RequestBody PrdFreeFlowVo vo) {
		List<String> messageList = ValidateFilter.getFilterMessage(vo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(vo)) {
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		}
		PrdFreeFlow prdFreeFlow = new PrdFreeFlow();
		BeanUtils.copyProperties(vo, prdFreeFlow);
		prdFreeFlow.setPomsType(1);
		prdFreeFlowService.save(prdFreeFlow);
		return ResultBean.ok(prdFreeFlow);
	}

	/**
	 * 判断是否重复
	 *
	 * @param prdFreeFlowVo 需要判断的vo
	 * @return 校验结果
	 */
	private boolean isRepeat(PrdFreeFlowVo prdFreeFlowVo) {
		PrdFreeFlow temp = new PrdFreeFlow();
		CopyUtils.copyProperties(prdFreeFlowVo, temp, "id");
		beanPropertiesUtil.setNullIfStringIsBlank(temp);
		List<PrdFreeFlow> list = prdFreeFlowService.findAll(Example.of(temp));
		for (PrdFreeFlow prdFreeFlow : list) {
			PrdFreeFlowVo vo = new PrdFreeFlowVo();
			PrdFreeFlowVo tempVo = new PrdFreeFlowVo();
			CopyUtils.copyProperties(prdFreeFlow, vo, "id");
			CopyUtils.copyProperties(prdFreeFlowVo, tempVo, "id");
			beanPropertiesUtil.setNullIfStringIsBlank(tempVo);
			beanPropertiesUtil.setNullIfStringIsBlank(vo);
			if (vo.equals(tempVo)) {
				if (prdFreeFlowVo.getId() == null || !Objects.equals(prdFreeFlowVo.getId(), prdFreeFlowVo.getId())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @param ids 多个时id以英文逗号分隔
	 * @return 商品包免流量删除
	 */
	@ApiOperation("商品包免流量 删除")
	@DeleteMapping(value = "/del")
	@MethodLog(module = "商品包免流量删除", insertDB = true, opterate = 3)
	public ResultBean del(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] idArr = ids.split(",");
			for (String id : idArr) {
				prdFreeFlowService.deleteById(Long.valueOf(id));
			}
			return ResultBean.ok();
		} else {
			return ResultBean.error("id不能为空");
		}
	}

	@ApiOperation(value = "商品包免流量 编辑", notes = "商品包免流量 编辑")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", required = true, value = "id"),
			@ApiImplicitParam(name = "pomsType", required = true, value = "1:产品包，2:节目"),
			@ApiImplicitParam(name = "pomsId", required = false, value = "商品包id"),
			@ApiImplicitParam(name = "yearFrom", required = false, value = "从何年开始"),
			@ApiImplicitParam(name = "yearTo", required = false, value = "到何年结束"),
			@ApiImplicitParam(name = "monthFrom", required = false, value = "从几月开始"),
			@ApiImplicitParam(name = "monthTo", required = false, value = "到几月结束(包含)"),
			@ApiImplicitParam(name = "weekFrom", required = false, value = "从周几开始"),
			@ApiImplicitParam(name = "weekTo", required = false, value = "到周几结束(包含)"),
			@ApiImplicitParam(name = "dayFrom", required = false, value = "从几日开始"),
			@ApiImplicitParam(name = "dayTo", required = false, value = "到几日结束(包含)"),
			@ApiImplicitParam(name = "timeFrom", required = false, value = "从几点开始"),
			@ApiImplicitParam(name = "timeTo", required = false, value = "到几点结束(包含)")
	})
	@PutMapping("/edit")
	@MethodLog(module = "商品包免流量编辑", insertDB = true, opterate = 2)
	public ResultBean edit(@RequestBody PrdFreeFlowVo prdFreeFlowVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(prdFreeFlowVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		PrdFreeFlow prdFreeFlow = new PrdFreeFlow();
		BeanUtils.copyProperties(prdFreeFlowVo, prdFreeFlow);
		PrdFreeFlow result = prdFreeFlowService.merge(prdFreeFlow);
		return ResultBean.ok(result);

	}

}
